package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.tablayout.SlidingTabLayout;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.OrderAgentRed;
import com.epfresh.supplier.fragment.OrderAgentFragment;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class OrderAgentActivity extends BaseActivity implements OnTabSelectListener {

    private SlidingTabLayout lyTab;
    private ViewPager vp;
    private OrderAgentFragment[] fragments = {new OrderAgentFragment("10"),new OrderAgentFragment("20"),new OrderAgentFragment("30"),new OrderAgentFragment("40"),new OrderAgentFragment("50"),new OrderAgentFragment()};
    private int currentFragmentIndex;
    int pageCount = 6;
    int orderType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_agent);
        ivBack.setVisibility(View.VISIBLE);
        initData(savedInstanceState);
        initView();
        requestOrderInfo();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.righticon:
                Intent search = new Intent(this, OrderAgentSearchActivity.class);
                startActivity(search);
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        refreshList();

    }

    private void initData(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            currentFragmentIndex = savedInstanceState.getInt(Constant.KEY_HOME_STATE_INDEX);
            getIntent().putExtra("orderType", savedInstanceState.getInt("orderType"));
        }

        orderType = getIntent().getIntExtra("orderType", 0);
    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        righticon.setImageResource(R.mipmap.order_search);
        righticon.setVisibility(View.VISIBLE);
        righticon.setOnClickListener(this);
        tvBack.setVisibility(View.VISIBLE);
        vp = (ViewPager) findViewById(R.id.vp);
        lyTab = (SlidingTabLayout) findViewById(R.id.ly_navigation);


        lyTab.setOnTabSelectListener(this);
        vp.setOffscreenPageLimit(pageCount);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentFragmentIndex = position;
                lyTab.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (fragments[position] == null)
                    fragments[position] = new OrderAgentFragment();
                currentFragmentIndex = position;
                return fragments[position];
            }

            @Override
            public int getCount() {
                return pageCount;
            }
        });
        String[] mTitles;
        setToolbarTitle("代办订单");
        mTitles = new String[]{"拼货", "付尾款", "发货", "收货", "结算", "全部"};

        lyTab.setViewPager(vp, mTitles);
        vp.setCurrentItem(currentFragmentIndex);
    }

    @Override
    public void onTabSelect(int position) {
        vp.setCurrentItem(position);
        if (position < fragments.length && fragments[position] != null) {
            fragments[position].refresh();
        }
    }

    //再次点击当前选中的tab
    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //记录当前fragment的下标
        outState.putInt(Constant.KEY_HOME_STATE_INDEX, currentFragmentIndex);
        outState.putInt("orderType", orderType);
        super.onSaveInstanceState(outState);
    }

    public void refreshList() {
        if (fragments[currentFragmentIndex] != null)
            fragments[currentFragmentIndex].refresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0x65 && resultCode == RESULT_OK) {
            refreshList();
        }
    }


    OnRequestListener<OrderAgentRed> onRequestListener = new OnRequestListener<OrderAgentRed>() {
        @Override
        public OrderAgentRed jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, OrderAgentRed.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
        }
        @Override
        public void onStart(Object tag) {
        }

        @Override
        public void onResponse(ResponseEntity<OrderAgentRed> response, Object tag) {
            hideProgressDialog();
            OrderAgentRed responseElement = response.getResponseElement();
            lyTab.showRedFlag(4,responseElement.isSettleCnt());
            lyTab.showRedFlag(3,responseElement.isReceiveGoodsCnt());
        }
    };
    String cmdOrderDetails = "pm-order/baseorder/groupon/agencyRedDot";

    public void requestOrderInfo() {
        RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
        entity.setCmd(cmdOrderDetails);
        Map<String, String> map = new HashMap<>();
        map.put("agencyStoreId",BaseApplication.getInstance().getStoreId());
        entity.setParameters(map);
        request(entity, cmdOrderDetails, onRequestListener);
    }

}
